package com.wise.esexample.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * TODO: 本demo，并没有使用
 * <p>
 * 这个工具类来自网上，通过手动拼接dsl_string语句 再去查询，可以用作参考
 */
public class DslQueryUtils {
    private static final String ASC = "asc";
    private static final String DESC = "desc";
    private static final String SOURCE = "{\"includes\": [ \"*\"],\"excludes\": [ \"track.*\" ]}";
    private static final String SOURCEAGGS = "{\"excludes\": [ \"*\" ]}";
    private static final String SOURCEAGGS_COLLAPSEC = "{\"excludes\": [ \"*\" ]}";

    public static String search(String dsl, String scriptField, String aggs, String source, String sort, String highLight, Integer from, Integer size, boolean profile) {
        StringBuilder stringBuilder = new StringBuilder("{");
        if (StringUtils.isNotBlank(dsl)) {
            //stringBuilder.append("\"query\":" + dsl + ",");
            stringBuilder.append("\"query\":").append(dsl).append(",");
        }
        if (StringUtils.isNotBlank(scriptField)) {
            //stringBuilder.append("\"script_fields\":" + scriptField + ",");
            stringBuilder.append("\"script_fields\":").append(scriptField).append(",");
        }
        if (StringUtils.isNotBlank(source)) {
            //stringBuilder.append("\"_source\":" + source + ",");
            stringBuilder.append("\"_source\":").append(source).append(",");
        }
        if (StringUtils.isNotBlank(sort)) {
            //stringBuilder.append("\"sort\":" + sort + ",");
            stringBuilder.append("\"sort\":").append(",");
        }
        if (StringUtils.isNotBlank(highLight)) {
            //stringBuilder.append("\"highLight\":" + highLight + ",");
            stringBuilder.append("\"highLight\":").append(highLight).append(",");
        }
        if (from != null) {
            //stringBuilder.append("\"from\":" + from + ",");
            stringBuilder.append("\"from\":").append(from).append(",");
        }
        if (size != null) {
            //stringBuilder.append("\"size\":" + size + ",");
            stringBuilder.append("\"size\":").append(size).append(",");
        }
        if (aggs != null) {
            //stringBuilder.append("\"aggs\":" + aggs + ",");
            stringBuilder.append("\"aggs\":").append(aggs).append(",");
        }
        if (profile) {
            //stringBuilder.append("\"profile\":" + profile + ",");
            stringBuilder.append("\"profile\":").append(",");
        }
        return stringBuilder.substring(0, stringBuilder.length() - 1) + "}";
    }

    public static String search(String dsl, String scriptField, String aggs, String collapse, String source, String sort, String highLight, Integer from, Integer size, boolean profile) {
        StringBuilder stringBuilder = new StringBuilder("{");
        if (StringUtils.isNotBlank(dsl)) {
            //stringBuilder.append("\"query\":" + dsl + ",");
            stringBuilder.append("\"query\":").append(dsl).append(",");
        }
        if (StringUtils.isNotBlank(scriptField)) {
            //stringBuilder.append("\"script_fields\":" + scriptField + ",");
            stringBuilder.append("\"script_fields\":").append(scriptField).append(",");
        }
        if (StringUtils.isNotBlank(source)) {
            //stringBuilder.append("\"_source\":" + source + ",");
            stringBuilder.append("\"_source\":").append(source).append(",");
        }
        if (StringUtils.isNotBlank(sort)) {
            //stringBuilder.append("\"sort\":" + sort + ",");
            stringBuilder.append("\"sort\":").append(sort).append(",");
        }
        if (StringUtils.isNotBlank(highLight)) {
            //stringBuilder.append("\"highLight\":" + highLight + ",");
            stringBuilder.append("\"highLight\":").append(highLight).append(",");
        }
        if (from != null) {
            //stringBuilder.append("\"from\":" + from + ",");
            stringBuilder.append("\"from\":").append(from).append(",");
        }
        if (collapse != null) {
            //stringBuilder.append("\"collapse\":" + collapse + ",");
            stringBuilder.append("\"collapse\":").append(collapse).append(",");
        }
        if (size != null) {
            //stringBuilder.append("\"size\":" + size + ",");
            stringBuilder.append("\"size\":").append(size).append(",");
        }
        if (aggs != null) {
            //stringBuilder.append("\"aggs\":" + aggs + ",");
            stringBuilder.append("\"aggs\":").append(aggs).append(",");
        }
        if (profile) {
            //stringBuilder.append("\"profile\":" + profile + ",");
            stringBuilder.append("\"profile\":").append(profile).append(",");
        }
        return stringBuilder.substring(0, stringBuilder.length() - 1) + "}";
    }

    public static String search(String dsl) {
        return search(dsl, null, null, null, null, null, null, null, false);
    }

    public static String search(String dsl, int from, int size) {
        return search(dsl, null, null, SOURCE, null, null, from, size, false);
    }

    public static String searchSort(String dsl, String sort, int from, int size) {
        return search(dsl, null, null, SOURCE, sort, null, from, size, false);
    }

    public static String search(String dsl, int from, int size, String source) {
        return search(dsl, null, source, null, null, null, from, size, false);
    }

    public static String searchAddScriptField(String dsl, String scriptField, int from, int size) {
        return search(dsl, scriptField, null, SOURCE, null, null, from, size, false);
    }

    public static String searchAddScriptField(String dsl, String sort, String scriptField, int from, int size) {
        return search(dsl, scriptField, null, SOURCE, sort, null, from, size, false);
    }

    public static String searchAggs(String dsl, String aggs, int from, int size) {
        return search(dsl, null, aggs, SOURCEAGGS, null, null, from, size, false);
    }

    public static String searchCollapse(String dsl, String collapse, int from, int size) {
        return search(dsl, null, null, collapse, SOURCEAGGS_COLLAPSEC, null, null, from, size, false);
    }

    /**
     * https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-post-filter.html
     *     * @param dsl
     *     * @param postFilter
     *     * @param aggs
     *     * @param from
     *     * @param size
     *     * @return
     *    
     */
    public static String searchPostFilter(String dsl, String postFilter, String aggs, int from, int size, String source) {
        return search(dsl, null, aggs, SOURCE, null, null, from, size, false);
    }

    public static String search(String dsl, String source, int from, int size) {
        if (StringUtils.isBlank(source)) {
            source = SOURCE;
        }
        return search(dsl, null, null, source, null, null, from, size, false);
    }

    public static String search(String dsl, String scriptfield, String source, String sort, int from, int size) {
        if (StringUtils.isBlank(dsl)) {
            dsl = "{}";
        }
        if (StringUtils.isBlank(source)) {
            source = SOURCE;
        }
        return "{\"query\":" + dsl + ",\"_source\":" + source + "\"sort\":" + sort + ",\"from\":" + from + ",\"size\":" + size + "}";
    }

    /**
     * https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html
     *    * @param query 正则通配符 widldcards
     *
     * @param operator and or not
     *                   * @param fields
     *                    * @return
     *                    
     */
    public static String queryStringOpr(String query, String operator, String... fields) {
        if (StringUtils.isBlank(operator)) {
            operator = "and";
        }
        StringBuilder sb = new StringBuilder();
        sb.append("{\"query_string\":{\"query\":\"" + query + "\",\"default_operator\":\"" + operator + "\",");
        int i;
        if (fields == null || fields.length == 0) {
            sb.append("\"default_field\":\"_all\",");
        } else {
            sb.append("\"fields\":[");
            for (i = 0; i < fields.length; i++) {
                sb.append("\"" + fields[i] + "\",");
            }
            sb = new StringBuilder(sb.substring(0, sb.length() - 1) + "],");
        }
        return sb.substring(0, sb.length() - 1) + "}}";
    }

    public static String queryString(String query, String... fields) {
        if (StringUtils.isBlank(query)) {
            return null;
        }
        return queryStringOpr(query, "and", fields);
    }

    public static String queryStringOr(String query, String... fields) {
        query = query.trim();
        if (StringUtils.isBlank(query)) {
            return null;
        }
        return queryStringOpr(query, "or", fields);
    }

    public static String scriptFields(String... values) {
        StringBuilder sb = new StringBuilder("{");
        for (int i = 0; i < values.length; i++) {
            sb.append("\"" + values[i] + "\":{");
            sb.append("\"script\":\"" + values[i + 1] + "\"},");
        }
        return sb.substring(0, sb.length() - 1) + "}";
    }

    public static String bool(String must) {
        return bool(must, null, null, null, null);
    }

    public static String bool(String must, String mustNot, String should) {
        return bool(must, mustNot, should, null, null);
    }

    public static String bool(String must, String mustNot, String should, int minimum_should_match) {
        return bool(must, mustNot, should, minimum_should_match, null);
    }

    public static String bool(String must, String mustNot, String should, String filter) {
        return bool(must, mustNot, should, null, filter);
    }

    public static String bool(String must, String mustNot, String should, Integer minimum_should_match, String filter) {
        StringBuilder sb = new StringBuilder("{\"bool\":{");
        if (StringUtils.isNotBlank(must)) {
            sb.append("\"must\":" + must + ",");
        }
        if (StringUtils.isNotBlank(mustNot)) {
            sb.append("\"must_not\":" + mustNot + ",");
        }
        if (StringUtils.isNotBlank(should)) {
            sb.append("\"should\":" + should + ",");
        }
        if (minimum_should_match != null) {
            sb.append("\"minimum_should_match\":" + minimum_should_match + ",");
        }
        if (StringUtils.isNotBlank(filter)) {
            sb.append("\"filter\":" + filter + ",");
        }
        return sb.substring(0, sb.length() - 1).toString() + "}}";
    }

    public static String boolFilter(String filter) {
        if (StringUtils.isBlank(filter))
            filter = "{}";
        return "{\"bool\":{\"filter\":" + filter + "}}";
    }

    /**
     * and or not
     * <p>
     *     * @param operator
     * <p>
     *     * @param dsl
     * <p>
     *     * @return
     * <p>
     *    
     */
    public static String filter(String operator, String dsl) {
        return "{\"" + operator + "\":[" + dsl + "]}";
    }

    public static String mergeDsl(String... dsls) {
        StringBuilder sb = new StringBuilder("[");
        for (String dsl : dsls) {
            if (StringUtils.isBlank(dsl)) {
                continue;
            }
            sb.append(dsl);
            sb.append(",");
        }
        return sb.substring(0, sb.length() - 1) + "]";
    }

    public static String match(String field, String value) {
        return "{\"match\":{\"" + field + "\":\"" + value + "\"}}";
    }

    /**
     * * @param field
     * * @param query
     * * @param operator (and or)
     * * @param minnum
     * * @return
     *    
     */
    public static String match(String field, String query, String operator, int minnum) {
        return "{\"match\":{\"" + field + "\":{\"query\":\"" + query + "\",\"operator\":\"" + operator + "\",\"\",:" + minnum + "}}";
    }

    public static String match(HashMap<String, String> map) {
        StringBuilder sb = new StringBuilder("[");
        for (Map.Entry entry : map.entrySet()) {
            if (entry.getValue() instanceof String) {
                sb.append("{\"match\":{\"" + entry.getKey() + "\":\"" + entry.getValue() + "\"}},");
            } else {
                sb.append("{\"match\":{\"" + entry.getKey() + "\":" + entry.getValue() + "\"}},");
            }
        }
        return sb.substring(0, sb.length() - 1) + "]";
    }

    public static String match(String field, Object... list) {
        StringBuilder sb = new StringBuilder("[");
        for (Object str : list) {
            if (str instanceof String) {
                sb.append("{\"match\":{\"" + field + "\":\"" + str + "\"}},");
            } else {
                sb.append("{\"match\":{\"" + field + "\":" + str + "\"}},");
            }
        }
        return sb.substring(0, sb.length() - 1) + "]";
    }

    public static String term(String type, Object value) {
        if (value == null || StringUtils.isBlank(String.valueOf(value))) {
            return null;
        }
        if (value instanceof String) {
            return "{\"term\":{\"" + type + "\":\"" + value + "\"}}";
        } else {
            return "{\"term\":{\"" + type + "\":" + value + "}}";
        }
    }

    public static String termCoverPlate(String type, String value) {
        if (StringUtils.isBlank(value)) {
            return null;
        }
        StringBuilder dsl = new StringBuilder("[");
        String[] types = value.split(",");
        for (String coverType : types) {
            dsl.append("{\"term\":{\"" + type + "\":" + coverType + "}},");
        }
        return dsl.substring(0, dsl.length() - 1) + "]";
    }

    public static String term(HashMap<String, String> map) {
        if (map.size() == 0) {
            return null;
        }
        StringBuilder sb = new StringBuilder("[");
        for (Map.Entry entry : map.entrySet()) {
            sb.append("{\"term\":{\"" + entry.getKey() + "\":\"" + entry.getValue() + "\"}},");
        }
        return sb.substring(0, sb.length() - 1) + "]";
    }

    public static String term(String field, String... values) {
        if (values == null || values.length == 0) {
            return null;
        }
        StringBuilder sb = new StringBuilder("[");
        for (String val : values) {
            sb.append("{\"term\":{\"" + field + "\":\"" + val + "\"}},");
        }
        return sb.substring(0, sb.length() - 1) + "]";
    }

    public static String muilMatch(String ctx, String... fields) {
        StringBuilder strBuilder = new StringBuilder("\"{\"multi_match\":{\"query\":\"\"+ctx+\"\",\"fields\":[");
        for (String filed : fields) {
            strBuilder.append("\"" + filed + "\",");
        }
        return strBuilder.substring(0, strBuilder.length() - 1) + "]}}}";
    }

    public static String muilMatchFuzzy(String ctx, String... fields) {
        StringBuilder strBuilder = new StringBuilder("\"{\"multi_match\":{\"query\":\"\"+ctx+\"\",\"fields\":[");
        for (String filed : fields) {
            strBuilder.append("\"" + filed + "\",");
        }
        return strBuilder.substring(0, strBuilder.length() - 1) + "],\"fuzziness\":\"AUTO\"}}}";
    }

    /**
     *     * @param field
     *     * @param value
     *     * @return
     *    
     */
    public static String wildCardQuery(String field, String value) {
        value = value + "*";
        return "{\"wildcard\":{\"" + field + "\",\"" + value + "\"}}";
    }

    public static String regexp(String field, String regexp) {
        return "{\"regexp\":{\"field\":{\"" + field + "\",\"" + regexp + "\"}}}";
    }

    /**
     * @param field
     * @param value     * @return
     *                 
     */
    public static String highlight(String field, String value) {
        return "{\"fields\":\"" + field + "\":{\"" + value + "\"}}";
    }

    public static String muilMatchAll(String ctx) {
        return "{\"multi_match\":{\"query\":\"" + ctx + "\",\"fields\":[\"_all\"]}}";
    }

    public static String matchAll() {
        return "{\"match_all\":{}}";
    }

    public static String must(String dsl) {
        return null;
    }

    public static String BuildermustNot(HashMap map) {
        return null;
    }

    public static String should(HashMap map) {
        return null;
    }

    public static String rangeMarkValue(HashMap<String, HashMap> markMap) {
        if (markMap.size() == 0) {
            return null;
        }
        StringBuilder sb = new StringBuilder("[");
        for (Map.Entry entry : markMap.entrySet()) {
            String type = entry.getKey().toString();
            Map mark = (Map) entry.getValue();
            String mk = String.valueOf(mark.get("mark"));
            int value = Integer.valueOf(String.valueOf(mark.get("value")));
            if ("=".equals(mk)) {
                sb.append("{\"term\":{\"" + type + "\":" + value + "}},");
            } else if (">".equals(mk)) {
                sb.append("{\"range\":{\"" + type + "\":{\"gt\":" + value + "}}},");
            } else if (">=".equals(mk)) {
                sb.append("{\"range\":{\"" + type + "\":{\"gte\":" + value + "}}},");
            } else if ("<".equals(mk)) {
                sb.append("{\"range\":{\"" + type + "\":{\"lt\":" + value + "}}},");
            } else if ("<=".equals(mk)) {
                sb.append("{\"range\":{\"" + type + "\":{\"lte" +
                        "\":" + value + "}}},");
            }
        }
        return sb.substring(0, sb.length() - 1) + "]";
    }

    public static String range(String type, Object minValue, Object maxValue, String gte, String lte) {
        Object tempMin = null;
        Object tempMax = null;
        if (minValue instanceof Integer) {
            tempMin = (Integer) minValue;
            tempMax = (Integer) maxValue;
        } else if (minValue instanceof Long) {
            tempMin = (Long) minValue;
            tempMax = (Long) maxValue;
        } else if (minValue instanceof String) {
            tempMin = "\"" + minValue + "\"";
            tempMax = "\"" + maxValue + "\"";
        } else if (minValue instanceof Double) {
            tempMin = (Double) minValue;
            tempMax = (Double) maxValue;
        } else if (minValue instanceof Float) {
            tempMin = (Float) minValue;
            tempMax = (Float) maxValue;
        } else if (minValue instanceof Date) {
            tempMin = ((Date) minValue).getTime();
            tempMax = ((Date) maxValue).getTime();
        }
        StringBuilder sb = new StringBuilder();
        sb.append("{\"range\":{\"" + type + "\":{");
        if (StringUtils.isNotBlank(gte)) {
            sb.append("\"" + gte + "\":" + String.valueOf(tempMin) + ",");
        }
        if (StringUtils.isNotBlank(lte)) {
            sb.append("\"" + lte + "\":" + String.valueOf(tempMax) + ",");
        }
        return sb.substring(0, sb.length() - 1) + "}}}";
    }

    public static String rangeGtAndLt(String type, Object minValue, Object maxValue) {
        return range(type, minValue, maxValue, "gt", "lt");
    }

    public static String rangeGteAndLte(String type, Object minValue, Object maxValue) {
        return range(type, minValue, maxValue, "gte", "lte");
    }

    public static String rangeGte(String type, Object minValue) {
        return range(type, minValue, null, "gte", null);
    }

    public static String rangeLte(String type, Object maxValue) {
        return range(type, null, maxValue, null, "lte");
    }

    public static String scriptQuery(String source) {
        return "{\"script\":{\"script\":\"" + source + "\"}}";
    }

    public static String scriptQueryByTime(String startTime, String endTime) {
        if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
            return null;
        }
        int start = Integer.valueOf(startTime.replace(":", ""));
        int end = Integer.valueOf(endTime.replace(":", ""));
        String source = "int hour = doc['pass_time'].date.hourOfDay;int minute  = doc['pass_time'].date.minuteOfHour;int flag=hour*100+minute; flag>=" + start + "&&flag <=" + end + " ";
        return "{\"script\":{\"script\":\"" + source + "\"}}";
    }

    public static String scriptQueryByTime2(String startTime, String endTime) {
        if (StringUtils.isBlank(startTime) || StringUtils.isBlank(endTime)) {
            return null;
        }
        if (startTime.length() == 4) {
            startTime = startTime + "00";
        }
        if (endTime.length() == 4) {
            endTime = endTime + "00";
        }
        int start = Integer.valueOf(startTime.replace(":", ""));
        int end = Integer.valueOf(endTime.replace(":", ""));
        return rangeGteAndLte("timenum", start, end);
    }

    /**
     * code
     *     * @param source
     *     * @param paramJson
     *     * @return
     *    
     */
    public static String scriptQuery(String source, String paramJson) {
        return "{\"script\":{\"script\":{\"code\":\"" + source + "\",\"lang\" :\"painless\",\"params\":" + paramJson + "}}}";
    }

    public static String scriptQueryByScriptId(String id, String paramJson) {
        return "{\"script\":{\"script\":{\"stored\":\"" + id + "\",\"params\":" + paramJson + "}}}";
    }

    public static String missing(String type) {
        return "{\"missing\":{\"field\":\"" + type + "\"}}";
    }

    public static String exists(String type) {
        return "{\"exists\":{\"field\":\"" + type + "\"}}";
    }

    public static String packageScript(String script) {
        return "{\"script\":{\"lang\":\"painless\",\"code\":\"" + script + "\"}}";
    }

    /**
     * 排序
     * <p>
     *     * @param field
     * <p>
     *     * @param sortType
     * <p>
     *     * @return
     * <p>
     *    
     */
    public static String sort(String field, String sortType) {
        if (StringUtils.isBlank(field)) {
            return null;
        }
        return "{ \"" + field + "\": { \"order\": \"" + sortType + "\" }}";
    }

    public static String asc(String field) {
        return sort(field, ASC);
    }

    public static String desc(String field) {
        return sort(field, DESC);
    }

    public static String hasParent(String type, String dsl) {
        return "{\"has_parent\":{\"type\":\"" + type + "\",\"query\":" + dsl + "}}";
    }

    public static String hasChild(String type, String dsl, String dsl2, boolean showChild) {
        StringBuilder sb = new StringBuilder("{\"has_child\":{ ");
        sb.append("\"type\":\"" + type + "\",\"query\":" + dsl + ",");
        if (showChild == true) {
            sb.append("\"inner_hits\": {} }");
        }
        String sql = sb.substring(0, sb.length() - 1) + "}";
        if (StringUtils.isNotBlank(dsl2)) {
            sql = sql + ",\"filter\":" + dsl2;
        }
        return sql + "}";
    }

    public static String hasChild(String type, String dsl, boolean showChild) {
        return hasChild(type, dsl, null, showChild);
    }

    public static String nestedQuery(String path, String dsl, boolean showNested) {
        return nestedQuery(path, dsl, null, showNested);
    }

    public static String nestedQuery(String path, String dsl) {
        return nestedQuery(path, dsl, null, true);
    }

    public static String nestedFilter(String path, String dsl) {
        return nestedQuery(path, null, dsl, true);
    }

    public static String nestedQuery(String type, String dsl, String dsl2, boolean showChild) {
        StringBuilder sb = new StringBuilder("{\"nested\":{ ");
        sb.append("\"path\":\"" + type + "\",\"query\":" + dsl + ",");
        if (showChild == true) {
            sb.append("\"inner_hits\": {} }");
        }
        String sql = sb.substring(0, sb.length() - 1) + "}";
        if (StringUtils.isNotBlank(dsl2)) {
            sql = sql + ",\"filter\":" + dsl2;
        }
        return sql + "}";
    }

    /**
     * Elasticsearch Fields _source 返回值字段设置
     * <p>
     *     * @param fields
     * <p>
     *     * @return
     * <p>
     *    
     */
    public static String source(String... fields) {
        StringBuilder sb = new StringBuilder("[");
        for (String field : fields) {
            sb.append("\"" + field + "\",");
        }
        return sb.substring(0, sb.length() - 1) + "]";
    }

    /**
     * 度量聚合
     *
     * @param nickName
     * @param aggsType
     * @return https://blog.csdn.net/donghaixiaolongwang/article/details/58597058
     * _search?search_type=count 参数：search_type=count 表示只返回aggregation部分的结果。
     * avg max min  sum cardinality(distinct)
     * stats 最大、最小、和、平均值。一起求出来
     * extended_stats 其他属性，包括最大最小，方差等等。
     * geo_bounds （attr "wrap_longitude" : true ） 计算出所有的地理坐标将会落在一个矩形区域
     * geo_centroid 计算出所有文档的大概的中心点
     * percentiles 百分比统计。可
     * percentile_ranks
     * top_hits  当于sql 中 group by 后取出前n条。
     * value_count  数量统计，看看这个字段一共有多少个不一样的数值。
     */
    public static String aggsMetrics(String nickName, String aggsType, String field) {
        return "{\"aggs\":{\"" + nickName + "\":{\"" + aggsType + "\":{\"field\":\"" + field + "\"}}}}";
    }

    public static String inner_hits(String name, String sort, int from, int size) {
        return "{\"name\":\"" + name + "\",\"sort\":" + sort + ",\"from\"" + from + "\",\"size\":" + size + "}";
    }

    /**
     * 自定义聚合
     * https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html
     *
     * @return
     */
    public static String scriptMetrics(String nickName, String init, String map, String combine, String reduce) {
        return "{\"" + nickName + "\":{" +
                "\"init_script\":" + init + "," +
                "\"map_script\":" + map + "," +
                "\"combine_script\":" + combine +
                "\"reduce_script\":" + reduce +
                "}}";
    }

    /**
     *     * @param values
     *     * @return
     *  
     */
    public static String aggsMetrics(String... values) {
        StringBuilder sb = new StringBuilder();
        sb.append("{\"aggs\":{");
        for (int i = 0; i < values.length; i++) {
            String nickName = values[i];
            String aggsType = values[i + 1];
            String field = values[i + 2];
            sb.append("\"" + nickName + "\":{\"" + aggsType + "\":{\"field\":\"" + field + "\"},");
        }
        return sb.substring(0, sb.length() - 1) + "}}";
    }

    /**
     * https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html
     *
     * @return adjacency_matrix 邻接矩阵,同事满足两个条件的数量
     * Children Aggregation 父子关系  聚合
     * Composite Aggregation
     * sub Aggregation
     * Nested Aggregation
     */
    public static String aggsBucket() {
        return null;
    }

    public static String aggsLine() {
        return null;
    }

    /**
     * 去除中括号,或者括号
     *     * @param json
     *     * @return
     *    
     */
    public static String wipeBracket(String json) {
        return StringUtils.isBlank(json) ? json : json.substring(1, json.length() - 1);
    }

    /**
     *    * @param sub
     *    * @return
     *    
     */
    public static String reduceChar(String sub) {
        return sub.replace("[", "").replace("]", "").replace(",", " ").replace("\"", "");
    }

    /**
     * @param sub
     * @return
     */
    public static String replaceChar(String sub) {
        return sub.replace("\"", "\'");
    }
}
